package petstore.web.servlet.search;

import petstore.domain.Item;
import petstore.domain.Product;
import petstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/Search")
public class SearchServlet extends HttpServlet {
    private String RESULT_PAGE="/WEB-INF/jsp/ResultPage/searchResult.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("searchName");
    //    System.out.println(name);
        CatalogService catalogService = new CatalogService();//实例化一个CatalogService对象
        List<Product> searchResult = catalogService.getProductListByName(name);
      //  System.out.println(searchResult.size());
        //向jsp传递
        HttpSession session = req.getSession();
        session.setAttribute("searchResult",searchResult);
        req.getRequestDispatcher(RESULT_PAGE).forward(req,resp);
    }
}
